<?php

namespace common\models;

use common\helpers\ArrayHelper;

/**
 * 账号登录基础模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
abstract class LoginBaseModel extends \common\base\Model {

    /**
     * @var \apiend\models\User 用户信息模型
     */
    private $_user = null;

    /**
     * 查询用户信息
     * @return \mpend\models\User|null
     */
    protected function getUser() {
        if ($this->_user === null) {
            $this->_user = $this->setUser();
        }
        return $this->_user;
    }

    /**
     * 校验用户是否存在
     * @param string $attribute the attribute currently being validated
     */
    public function validateUser($attribute, $params) {
        if ($this->hasErrors()) {
            return;
        }
        if (!$this->getUser()) {
            $message = ArrayHelper::getValue($params, 'message', '未找到用户数据信息');
            $this->addError($attribute, $message);
        }
    }

    abstract protected function setUser();

    /**
     * Resolves the current request into a route and the associated parameters.
     * @return array the first element is the route, and the second is the associated parameters.
     */
    abstract public function login();
}
